import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
/*
 * MapView.java
 *
 * $LastChangedDate: 2008-05-29 10:40:20 +0000 (Thu, 29 May 2008) $
 * $LastChangedRevision: 37 $
 * Vicente J. Ferrer Dalmau
 * < vicente@jdalmau.es >
 *
 */

/**
 * This class displays the graphical state of a Map
 * @author Vicente J. Ferrer Dalmau
 */
public class MapView extends JPanel {
    
    Map MAP;
    
    /** Creates a new instance of MapView */
    public MapView(Map map) {
    
        MAP = map;
    }
    
    /* Displays the current state of the Map */
    public void displayMap() {
        
         // sets a GridLayout
        GridLayout layout = new GridLayout(MAP.SIZE, MAP.SIZE);
        layout.setHgap(1);
        layout.setVgap(1);
        this.setLayout(layout);                
        
        // sets a border
        this.setBorder(new BevelBorder(BevelBorder.RAISED));
        
        // now append the Zones to the Map in the JPanel
        for (int row = 0; row<MAP.SIZE; row++)
            for(int column = 0; column<MAP.SIZE; column++) {
                Zone zone = MAP.board[row][column];
                if (zone.NUMBER == -1) {
                    MapViewWater jp = new MapViewWater();
                    jp.setBorder(new LineBorder(Color.black));
                    jp.setLayout(new FlowLayout());
                    
                    this.add(jp);
                }
                else {
                    JPanel jp = new JPanel();
                    JLabel l = new JLabel();
                    l.setText("");
                    l.setForeground(Color.WHITE);
                    jp.add(l);
                    jp.setToolTipText("Zona "+zone.NUMBER);
                    jp.setBorder(new LineBorder(Color.black));
                    jp.setLayout(new FlowLayout());
                    if (zone.color == null) {                        
                        jp.setBackground(ColorsList.earth.color);                        
                    }
                    else {                        
                        jp.setBackground(zone.color.color);
                    }
                    if (zone.constraints.size() > 0) {
                        ((JLabel)jp.getComponent(0)).setText("R");
                        jp.setToolTipText(jp.getToolTipText()+": ");
                        for (int i=0; i<zone.constraints.size(); i++) {
                            MyColor c = (MyColor)zone.constraints.get(i);
                            jp.setToolTipText(jp.getToolTipText()+c.name+", ");
                        }
                    }
                    this.add(jp);
                }
            }
    }        
    
    public class MapViewWater extends JPanel {        
       MapViewWater () {
           super();
       }       
       /** Override the paintComponent method to display a background image */
        public void paintComponent(Graphics g) {            
            Dimension size = getSize();
            ImageIcon back = new ImageIcon(getClass().getResource("images/water.jpg"));
            g.drawImage(back.getImage(), 0, 0, size.width, size.height, null);
            setOpaque(false);
            super.paintComponent(g);
        }
    }                
}
